/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.jboss.fuse.examples.cxf.jaxws;

import java.util.ArrayList;
import java.util.List;
import javax.xml.ws.Holder;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.component.cxf.CxfConstants;
import org.apache.cxf.message.MessageContentsList;

/**
 *
 * @author Administrator
 */
public class HelloWorldProcessor implements Processor {

    public void process(Exchange exchng) throws Exception {
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        String operationName = exchng.getIn().getHeader(CxfConstants.OPERATION_NAME, String.class);
        MessageContentsList msgList = (MessageContentsList) exchng.getIn().getBody();
        if (operationName == "sayHi") {
            String name = (String) msgList.get(0);
            String tmp = "Hello ";
            tmp = tmp + name;
            exchng.getOut().setBody(tmp);

        } else if (operationName == "getPerson") {
            String name = (String) msgList.get(0);
            Person p = new Person();
            p.setName(name);
            exchng.getOut().setBody(p);

        } else if (operationName == "getPersonList") {
            List<String> names = (List<String>) msgList.get(0);
            //ArrayList<Holder<Person>> pl = new ArrayList<Holder<Person>>();
            ArrayList<Person> pls = new ArrayList<Person>();
            for (String s : names) {
                Person p = new Person();
                p.setName(s);
                //Holder<Person> hp = new Holder<Person>(p);
                //pl.add(hp);
                pls.add(p);
            }
            exchng.getOut().setBody(pls.toArray(new Person[pls.size()]));
        } else if (operationName == "getPersonArray") {
            List<String> names = (List<String>) msgList.get(0);
           // ArrayList<Holder<Person>> pl = new ArrayList<Holder<Person>>();
            ArrayList<Person> pls = new ArrayList<Person>();
            for (String s : names) {
                Person p = new Person();
                p.setName(s);
                
                pls.add(p);
            }
            PersonArray pa=new PersonArray();
            pa.setPersons(pls.toArray(new Person[pls.size()]));
             exchng.getOut().setBody(pa);

        } else {
        }

    }

}
